//
//  GFUploadImgNetWork.m
//  lib.utils
//
//  Created by 胡林虎 on 2017/6/29.
//  Copyright © 2017年 胡林虎. All rights reserved.
//

#import "GFUploadImgNetWork.h"
#import <AFNetworking/AFNetworking.h>
#import <objc/runtime.h>

static char upLoadImage;

@implementation GFUploadImgNetWork

-(void)uploadImage:(UIImage *)image {

    objc_setAssociatedObject(self, &upLoadImage, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (AFConstructingBlock)constructingBodyBlock {
    
    return ^(id<AFMultipartFormData> formData) {
        
        NSData *imageData =UIImageJPEGRepresentation(objc_getAssociatedObject(self, &upLoadImage) ,0.8);
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        formatter.dateFormat =@"yyyyMMddHHmmss";
        NSString *str = [formatter stringFromDate:[NSDate date]];
        NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];
        //上传的参数(上传图片，以文件流的格式)
        [formData appendPartWithFileData:imageData
                                    name:@"file"
                                fileName:fileName
                                mimeType:@"image/jpeg"];
    };
}

#pragma mark -
#pragma mark - 通过runtime 设置属性

-(void)setImage:(UIImage *)image {
    objc_setAssociatedObject(self, &upLoadImage, image, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIImage *)image{
    return objc_getAssociatedObject(self, &upLoadImage);
}


@end
